//
//  HWNewH5ViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2020/9/23.
//  Copyright © 2020 Chendaobin. All rights reserved.
//

#import "HWNewH5ViewController.h"
#import "HWmouxingModel.h"
#import "HWHomezhongyaodeCell.h"

@interface HWNewH5ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) NSMutableArray *gameArr;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSString *p;


@end

@implementation HWNewH5ViewController

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-CF_NavHeight-44-CF_TabbarHeight) style:UITableViewStyleGrouped];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.backgroundColor = RGB(247, 247, 247);


        
    }
    return   _mainaTableVie ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.view.backgroundColor = [UIColor  redColor];
    
      [self setupUI];
    
    
     [self addrefreshControl];
    
}


- (void)setupUI {
    
    
  //   self.gameArr = [[NSMutableArray alloc]init];
    
    [self.view addSubview:self.mainaTableVie];
    
    
}

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
     self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

#pragma  mark --------- 下拉刷新
- (void)underRefresh {
    
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        // 结束刷新
    //        [self.mainaTableVie.mj_header endRefreshing];
    //    });
    
    
    // [SVProgressHUD show];
    
    self.p = @"1";

       __weak typeof(self) weakSelf = self;
    
      self.gameArr = [[NSMutableArray alloc]init];
    
        //    NSString *userID = [HWLocallyStoredData getLoginUserID];
            NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
             NSString *sign = [NSString stringWithFormat:@"%@%@",tStr,@"fsd213ewdsadqwe2121213edsad"];
             NSString *signMD5 = [WMUtil md5HexDigest:sign];
             NSString *typeStr =  @"5";
    
       
             NSDictionary *parametes = @{
                                      
                                        @"ios" : @"1",
                                        @"t" :tStr,
                                        @"sign": signMD5,
                                        @"type" : self.typeStr,
                                        @"p" : self.p,
                                         @"member_id":[HWLocallyStoredData getCode],
                                        @"language":[HWjianzhiduiObject returnLanguageAcquisition],
                        
                            
                                        };
    
    [HWWebUtils do4GetWithParameters:parametes  hiddenparameterStr:HW_index  progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

        if ([codeStr intValue] == 1) {
            
            NSArray *arr1 =  response[@"data"][@"top"];

            NSArray *arr2 =  response[@"data"][@"list"];
            
            
            
            NSMutableArray *mutableArr1 =    [HWmouxingModel mj_objectArrayWithKeyValuesArray:arr1];
            NSMutableArray *mutableArr2 =    [HWmouxingModel mj_objectArrayWithKeyValuesArray:arr2];
            
            
            [self.gameArr addObject:mutableArr1];
            [self.gameArr addObject:mutableArr2];
            
            
            self.p = response[@"data"][@"p"];
            self.max_p = response[@"data"][@"max_p"];
            
            
            [self.mainaTableVie reloadData];
            
            
            
            
           
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
}




- (void)onRefresh {
    
    
    if ([self.p  intValue] > [self.max_p intValue]) {
        
        [MBProgressHUD  showError:@"请稍后加载数据"];
        
        [self.mainaTableVie.mj_header endRefreshing];
              [self.mainaTableVie.mj_footer endRefreshing];
        return;
    }
    
    
    
    
    
        NSString *userID = [HWLocallyStoredData getLoginUserID];
            NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
             NSString *sign = [NSString stringWithFormat:@"%@%@",tStr,@"fsd213ewdsadqwe2121213edsad"];
             NSString *signMD5 = [WMUtil md5HexDigest:sign];
            NSString *typeStr =  @"5";
    
       
             NSDictionary *parametes = @{
                                    
                                        @"ios" : @"1",
                                        @"t" :tStr,
                                        @"sign": signMD5,
                                        @"type" : self.typeStr,
                                        @"p" : self.p,
                                        @"member_id":[HWLocallyStoredData getCode],
                                        @"language":[HWjianzhiduiObject returnLanguageAcquisition],
                        
                            
                                        };
    
    [HWWebUtils do4GetWithParameters:parametes  hiddenparameterStr:HW_index progress:nil success:^(id response) {
        
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];

        if ([codeStr intValue] == 1) {
            
         //    NSArray *arr1 =  response[@"data"][@"top"];

            NSArray *arr2 =  response[@"data"][@"list"];
            
            
            
           //  NSMutableArray *mutableArr1 =    [HWmouxingModel mj_objectArrayWithKeyValuesArray:arr1];
            NSMutableArray *mutableArr2 =    [HWmouxingModel mj_objectArrayWithKeyValuesArray:arr2];
            
            
            // [self.gameArr addObject:mutableArr1];
            
           //  [self.gameArr addObject:mutableArr2];
            
            NSMutableArray *dataMuarr =  self.gameArr[1];
            
            for (int  i = 0; i <  mutableArr2 .count ; i++) {
        
                
                [dataMuarr addObject:mutableArr2[i]];
                
                
            }
        
            
            
            
            self.p = response[@"data"][@"p"];
            self.max_p = response[@"data"][@"max_p"];
            [self.mainaTableVie reloadData];
            
            
            
            
           
        }
        
        
        
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
    }];
    
    
    
    
    
    
    
    
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    
    return  self.gameArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return   [self.gameArr[section] count];
    
   
    
    
    
    
    
    
}

 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *cellID = @"cellID";
    
    HWHomezhongyaodeCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"HWHomezhongyaodeCell" owner:nil options:nil].firstObject;
        
    }
    HWmouxingModel  *model = self.gameArr[indexPath.section][indexPath.row];
    cell.tuijianModel = model;
    cell.xiantiaoView.hidden = YES;
    
    cell.nameLabel.textColor = RGB(68, 68, 68);
    
    if (indexPath.section  == 0 ) {
        
        if (indexPath.row == 0 ) {
               
               cell.newbeijingiamgeView.hidden = NO;
               
               cell.xiantiaoView.hidden = NO;
              //  cell.xiantiaoView.hidden = YES;
               cell.beijingshituView.backgroundColor = [UIColor clearColor];
               [cell.beijingtupianIangeview sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:nil];
               
               cell.jianjutupian.constant = 20;
               cell.topjianju.constant = 46;
               cell.nameLabel.textColor = RGB(255, 255, 255); 
               
                
           } else {
               
                 cell.newbeijingiamgeView.hidden = YES;
                cell.beijingshituView.backgroundColor = [UIColor whiteColor];
               
                cell.jianjutupian.constant = 0;
                 cell.topjianju.constant = 31;
               
           }
        
    } else {
        
        cell.newbeijingiamgeView.hidden = YES;
        cell.beijingshituView.backgroundColor = [UIColor whiteColor];
                      
        cell.jianjutupian.constant = 0;
        cell.topjianju.constant = 31;
        
    }
          
  cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    
    return  cell;
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
   
    
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0 ) {
             
            return 162+36+20;
            
        }
         
    }
    
    return  78;
    
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    
    return  10+56;
    
   
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 20;
    
    
    
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    
    UIView  *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
    view.backgroundColor = RGB(247, 247, 247);
    
    
    
    UIView *baiseView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, kScreenW-20, 56)];
    baiseView.backgroundColor = RGB(255, 255, 255);
    
    [view addSubview:baiseView];
    
    
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:baiseView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = baiseView.bounds;
        maskLayer.path = maskPath.CGPath;
        baiseView.layer.mask = maskLayer;
    
    
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(12, 24,4,16)];
        
        view1.backgroundColor = RGB(248, 119, 6);
    //    view1.layer.cornerRadius = 10;
    //    view1.layer.masksToBounds = YES;
    //
        [baiseView addSubview:view1];
        
    
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(view1.right+6,20, 200, 26)];
               //     nameLabel.text = fanlilistArr[i];
        //  nameLabel.backgroundColor =  RGB(248, 119, 6);
         nameLabel.backgroundColor = [UIColor clearColor];
          nameLabel.textAlignment = NSTextAlignmentLeft;
          nameLabel.font = [UIFont systemFontOfSize:18];
          nameLabel.textColor = RGB(68, 68, 68);
    
   nameLabel.text = @"平台活动";
 //    nameLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:20];//又粗又斜
     //     nameLabel.layer.cornerRadius = 8;
     //     nameLabel.layer.masksToBounds = YES;
    UIFontDescriptor * fontD = [nameLabel.font.fontDescriptor
    fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold
                    | UIFontDescriptorTraitItalic];
    nameLabel.font = [UIFont fontWithDescriptor:fontD size:0];

          nameLabel.text = @"平台活动";
       [baiseView addSubview:nameLabel];
    
    if (section == 0) {
        nameLabel.text = @"H5风云榜";
        nameLabel.text = [HWjianzhiduiObject keylocalization:@"H5风云榜"];
        
        
    } else {
        
        
         nameLabel.text = @"全部游戏";
        nameLabel.text = [HWjianzhiduiObject keylocalization:@"全部游戏"];
    }
    
    

    
    
    return  view;
    
    
    
    
    
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    
    
    
    
    UIView  *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 20)];
    view.backgroundColor =RGB(247, 247, 247);
    
    
    UIView *baiseView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, kScreenW-20, 20)];
      baiseView.backgroundColor = RGB(255, 255, 255);
      
      [view addSubview:baiseView];
      
      
          UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:baiseView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
          CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
          maskLayer.frame = baiseView.bounds;
          maskLayer.path = maskPath.CGPath;
          baiseView.layer.mask = maskLayer;
    
    
    
    
    return  view;
    
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
     HWmouxingModel  *model = self.gameArr[indexPath.section][indexPath.row];
    
    
    
      HWGameDetailController *game = [[HWGameDetailController alloc]init];
       game.gameId = model.id;
      [[[HWViewUtils  getCurrentVC] navigationController]  pushViewController:game animated:YES];
       
    
    
    
}
@end
